use std::{
ffi::{CStr, CString, NulError},
os::raw::c_char,
ptr,
};
use crate::types::ParameterNameRef;
use crate::VoicemeeterRemote;
impl VoicemeeterRemote {
pub fn is_parameters_dirty(&self) -> Result<bool, IsParametersDirtyError> {
let res = unsafe { self.raw.VBVMR_IsParametersDirty() };
match res {
0 => Ok(false),
1 => Ok(true),
-1 => Err(IsParametersDirtyError::CannotGetClient),
-2 => Err(IsParametersDirtyError::NoServer),
s => Err(IsParametersDirtyError::Other(s)),
}
}
#[tracing::instrument(skip(self))]
pub fn get_parameter_float(&self, param: &ParameterNameRef) -> Result<f32, GetParameterError> {
let mut f = 0.0f32;
let param = CString::new(param.as_ref())?;
tracing::debug!("getting float parameter");
let res = unsafe {
self.raw
.VBVMR_GetParameterFloat(param.as_ptr() as *mut _, &mut f)
};
match res {
0 => Ok(f),
-1 => Err(GetParameterError::CannotGetClient),
-2 => Err(GetParameterError::NoServer),
-3 => Err(GetParameterError::UnknownParameter(
param.to_string_lossy().into_owned(),
)), -5 => Err(GetParameterError::StructureMismatch(
param.to_string_lossy().into_owned(),
"float",
)),
s => Err(GetParameterError::Other(s)),
}
}
#[tracing::instrument(skip(self))]
pub fn get_parameter_string(
&self,
param: &ParameterNameRef,
) -> Result<String, GetParameterError> {
let param = CString::new(param.as_ref()).unwrap();
let mut output = [0 as c_char; 512];
tracing::debug!("getting string parameter");
let res = unsafe {
self.raw
.VBVMR_GetParameterStringA(param.as_ptr() as *mut _, ptr::addr_of_mut!(output[0]))
};
match res {
0 => {
let output = unsafe { CStr::from_ptr(ptr::addr_of!(output[0])) }
.to_string_lossy()
.into_owned();
Ok(output)
}
-1 => Err(GetParameterError::CannotGetClient),
-2 => Err(GetParameterError::NoServer),
-3 => Err(GetParameterError::UnknownParameter(
param.to_string_lossy().into_owned(),
)), -5 => Err(GetParameterError::StructureMismatch(
param.to_string_lossy().into_owned(),
"float",
)),
s => Err(GetParameterError::Other(s)),
}
}
}
#[derive(Debug, thiserror::Error, Clone)]
#[non_exhaustive]
pub enum GetParameterError {
#[error("could not make into a c-string")]
NulError(#[from] NulError),
#[error("error (unexpected)")]
CannotGetClient,
#[error("no server")]
NoServer,
#[error("unknown parameter: {0}")]
UnknownParameter(String),
#[error("tried to parse parameter {0:?} as a {1} but it is not")]
StructureMismatch(String, &'static str),
#[error("unexpected error occurred: error code {0}")]
Other(i32),
}
#[derive(Debug, thiserror::Error, Clone)]
#[non_exhaustive]
pub enum IsParametersDirtyError {
#[error("error (unexpected)")]
CannotGetClient,
#[error("no server")]
NoServer,
#[error("unexpected error occurred: error code {0}")]
Other(i32),
}